<?php
/** .-------------------------------------------------------------------
* |    Author: 熊伟洋 <chelious@foxmail.com>
* |    WeChat: hello_McGrady
* |    	   QQ: 434493420
* |     Motto: Hungry & Humble
* |---------------------------------------------------------------------
* |    Copyright (c) 2012-2020, www.chelious.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace app\admin\controller;

class Category extends Common{
    private $model;

    public function __construct()
    {
        $this->model = new \system\model\Category();
        parent::__construct();
    }

    //加载模版
    public function index(){

        /*$datapage = Db::table('category')->paginate(6);
        $page = $datapage->links();
        $datapage = $datapage->toArray();
        $data = Data::tree($datapage,'cname','cid','pid');
        View::with(['data' => $data , 'datapage' => $datapage , 'page' => $page]);*/

        $data = Db::table('category')->get();
        $data = Data::tree($data,'cname','cid','pid');
        View::with(['data' => $data]);

        return view();
    }

    //添加分类
    public function add(){
        /*$data = Db::table('category')->get();
        $data = Data::tree($data,'cname','cid','pid');
        View::with(['data' => $data]);*/

        if(IS_POST){
            if($this->model->add()){
                //添加分类成功
                message('添加分类成功',u('admin.category.index'),'success');
            }else{
                //添加分类失败
                message($this->model->getError(),'back','error');
            }

        }
        return view();
    }

    //添加子分类
    public function addSon(){
        $cid = Q('get.cid');
        $data = Db::table('category')->where('cid',$cid)->field(['cid','cname'])->first();
        View::with('data',$data);
        if(IS_POST){
            if($this->model->add()){
                //添加分类成功
                message('添加子分类成功',u('admin.category.index'),'success');
            }else{
                //添加分类失败
                message($this->model->getError(),'back','error');
            }
        }

        return view();
    }

    //编辑
    public function edit(){
        $cid = Q('get.cid');
//        p($cid);die;
        $thisData = Db::table('category')->where('cid',$cid)->first();
//        p($thisData);die;

        $data = Db::table('category')->whereNotIn('cid',[$cid])->get();
        $data = Data::tree($data,'cname','cid','pid');
//        p($data);die;
        View::with(['data' => $data,'thisData' => $thisData]);

        if(IS_POST){
            if($this->model->edit()){
                //添加分类成功
                message('编辑分类成功',u('admin.category.index'),'success');
            }else{
                //添加分类失败
                message('编辑分类失败','back','error');
            }
        }

        return view();
    }

    //删除
    public function del(){
        $cid = Q('get.cid');
        /*$rows = Db::table('category')->where('cid',$cid)->delete();
        if($rows){
            //添加分类成功
            message('删除分类成功',u('admin.category.index'),'success');
        }else{
            //添加分类失败
            message('删除分类失败','back','error');
        }*/

        /***************/
        $rows = $this->auto($cid);
        //先递归删除所有子元素，最后再删除本身
        $rows = Db::table('category')->where('cid',$cid)->delete();
        if($rows){
            //添加分类成功
            message('删除分类成功',u('admin.category.index'),'success');
        }else{
            //添加分类失败
            message('删除分类失败','back','error');
        }

    }

    //递归删除方法
    public function auto($cid){
        $data = Db::table('category')->get();
//        p($data);
        foreach($data as $k => $v){
//            p($v);
            if($v['pid'] == $cid){
//                p($v);
                $this->auto($v['cid']);
                //先删最底层的分类
                Db::table('category')->where('cid',$v['cid'])->delete();
            }
        }

    }


}
